package com.tsinghuabigdata.edu.ddmath.adapter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.tsinghuabigdata.edu.ddmath.R;
import com.tsinghuabigdata.edu.ddmath.bean.LoginInfo;
import com.tsinghuabigdata.edu.ddmath.util.AccountUtils;
import com.tsinghuabigdata.edu.ddmath.util.AppLog;
import com.tsinghuabigdata.edu.ddmath.util.PicassoUtil;
import com.tsinghuabigdata.edu.ddmath.view.RoundImageView;

import java.io.File;
import java.net.URLEncoder;
import java.util.List;


/**
 * Created by Administrator on 2017/3/3.
 */

public class PreviewBaseAdapter extends BaseAdapter {

    private int          seclectPosition;
    private Context      mContext;
    private List<String> mDatas;
    private String       accessToken;
    private int          curPosition;

    public PreviewBaseAdapter(Context context, List<String> list, int position) {
        mDatas = list;
        mContext = context;
        seclectPosition = position;
        curPosition = position;
        LoginInfo loginInfo = AccountUtils.getLoginUser();
        if (loginInfo != null) {
            try {
                accessToken = URLEncoder.encode(loginInfo.getAccessToken(), "utf-8");
            } catch (Exception e) {
                AppLog.d(e.toString());
            }
        }
    }


    @Override
    public int getCount() {
        if (mDatas != null) {
            return mDatas.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (mDatas != null) {
            return mDatas.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;

        if (convertView == null) {
            // 没有复用
            convertView = View.inflate(mContext, R.layout.item_picture_preview, null);
            holder = new ViewHolder();
            convertView.setTag(holder);
            // 给holder找view
            holder.tv = (TextView) convertView.findViewById(R.id.tv_preview_position);
            holder.imageView = (RoundImageView) convertView.findViewById(R.id.iv_preview);
//            holder.linearLayout = (LinearLayout) convertView.findViewById(R.id.ll_iv_preview);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        String url = mDatas.get(position);
        // 给holder中的view设置数据
        holder.tv.setText(String.valueOf(position + 1));
        if (url.startsWith("http")) {
            String comUrl = url;// + "?access_token=" + accessToken;
            Log.i("grubbyurl", "comUrl=" + comUrl);
            PicassoUtil.getPicasso(mContext).load(comUrl).error(R.drawable.ic_broken_image).placeholder(R.drawable.ic_temporary_image).into(holder.imageView);
        } else {
            Picasso.with(mContext).load(new File(url)).error(R.drawable.ic_broken_image).placeholder(R.drawable.ic_temporary_image).into(holder.imageView);
        }
        if (position == curPosition) {
            holder.linearLayout.setBackgroundResource(R.drawable.bg_preview_select);
        } else {
            holder.linearLayout.setBackgroundResource(R.drawable.bg_preview_unselect);
        }


        return convertView;
    }

    class ViewHolder {
        TextView       tv;
        RoundImageView imageView;
        LinearLayout   linearLayout;
    }

    public void select(int position) {
        if (position != curPosition) {
            curPosition = position;
            notifyDataSetChanged();
        }
    }
}

